package com.augurit.gzcgwsp.wfex.bi.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX;
import com.augurit.wf.bi.web.WfBusTemplateAction;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;


@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "wf-bus-template-ex.action", type = "redirect"),//城管委修改，增加-ex
	@Result(name = CrudActionSupport.SUCCESS, location = "/wf/bi/bus-template/bus-template-mgmt.jsp"),
	@Result(name = WfBusTemplateEXAction.DRAFT_VIEW, location = "/gzcgwsp/wfex/client/common/draft-view.jsp")//城管委添加
})
public class WfBusTemplateEXAction  extends WfBusTemplateAction{
	//城管委添加，开始
	public static final String DRAFT_VIEW = "DRAFT_VIEW";			//起草页面
	
	@Autowired
	private IWfBusTemplateServiceEX wfBusTemplateServiceEX;
	
	private List<WfBusTemplateForm> list;
	
	/**
	 * 点击窗口收案，调用方法
	 * @return
	 * @throws Exception
	 */
	public String loadDraftView() throws Exception {
		LoginUserForm loginUserForm = this.getLoginUser();
		//list = wfBusTemplateService.getAllActive();
        list = new ArrayList<WfBusTemplateForm>();
        Map<String, List<Map<String, String>>> tplcodeMap = new HashMap<String, List<Map<String, String>>>();
        List<WfBusTemplateForm> AllList = wfBusTemplateServiceEX.getAllActive(loginUserForm);
        if(AllList != null){
        	for (WfBusTemplateForm tplForm : AllList) {
                int _index = tplForm.getTemplateCode().indexOf("_");
                if (_index > 0) {
                    String tplCode = tplForm.getTemplateCode().substring(0, _index);
                    List<Map<String, String>> tplList = tplcodeMap.get(tplCode);
                    
                    Map<String, String> elmap = new HashMap<String, String>();
                    elmap.put(tplForm.getTemplateCode(), tplForm.getTemplateName().substring(tplForm.getTemplateName().indexOf("==>") + 3));
                    if (tplList == null) {
                        tplList = new ArrayList<Map<String, String>>();
                        WfBusTemplateForm tform = new WfBusTemplateForm();
                        tform.setTemplateCode(tplCode);
                        tform.setTemplateName(tplForm.getTemplateName().substring(0, tplForm.getTemplateName().indexOf("==>")));
                        tform.setTemplateTypeName(tplForm.getTemplateTypeName());
                        
                        list.add(tform);
                    }
                    
                    tplList.add(elmap);
                    tplcodeMap.put(tplCode, tplList);
                } else {
                    list.add(tplForm);
                }
            }
        }        
        BaseAction.getRequest().setAttribute("TplMap", JSONObject.fromObject(tplcodeMap));
		return DRAFT_VIEW;
	}
	
	public List<WfBusTemplateForm> getList() {
		return list;
	}

	public void setList(List<WfBusTemplateForm> list) {
		this.list = list;
	}
	//城管委添加，结束
}
